<?php

if_get('/estates', function ()
{
    list(
        $inputs['name'], $inputs['output_address_slot_id']
    ) = input_list(
        'name', 'output_address_slot_id'
    );
    $inputs = array_filter($inputs, 'not_null');

    return render('estate/list', [
        'estates' => dao('estate')->find_all_by_column($inputs),
    ]);
});

if_get('/estates/add', function ()
{
    return render('estate/add');
});

if_post('/estates/add', function ()
{
    $estate = estate::create();

    $estate->name = input('name');
    $estate->is_auto_manage_value = input('is_auto_manage_value');
    $estate->output_address_slot_id = input('output_address_slot_id');

    return redirect('/estates');
});

if_post('/estates/upload', function ()
{
    $file_info = input_file('file');

    $file_path = $file_info['tmp_name'];

    $file_md5 = md5_file($file_path);

    $excel = PHPExcel_IOFactory::load($file_path);

    otherwise($excel->getSheetCount() === 1, '上传的表格中有太多 sheet');

    $excel->setActiveSheetIndex(0); // 选中第一个 sheet

    $active_sheet = $excel->getActiveSheet();

    $row_count = $active_sheet->getHighestRow();
    $col_count = $active_sheet->getHighestColumn();

    for ($col = 'A'; $col <= $col_count; $col ++) {

        $keys[$col] = $active_sheet->getCell($col.'1')->getValue();
    }

    for ($row = 2; $row < $row_count; $row++) {

        $data_info = [];

        for ($col = 'A'; $col <= $col_count; $col ++) {

            $data_info[$keys[$col]] = $active_sheet->getCell($col.$row)->getValue();
        }

        otherwise(
            isset($data_info['communityName'])
            && isset($data_info['id'])
            && isset($data_info['name'])
            && isset($data_info['parentId'])
            , '表头不完整，请参考导入模版');

        otherwise(
            $data_info['communityName']
            && $data_info['id']
            && $data_info['name']
            && $data_info['parentId']
            , '第'.$row.'行数据不完整');
    }

    return redirect('/estates');
});

if_get('/estates/detail/*', function ($estate_id)
{
    $estate = dao('estate')->find($estate_id);
    otherwise($estate->is_not_null(), 'estate not found');

    $output_address_slot = $estate->output_address_slot;

    $address_unit_value_infos = [];

    if ($output_address_slot->is_not_null()) {

        $address_unit_turns = $output_address_slot->address_unit_turns;

        $address_units = relationship_batch_load($address_unit_turns, 'address_unit');

        $address_unit_values = relationship_batch_load($address_unit_turns, 'address_unit_values');

        $tree_address_unit_turn_index = [];

        foreach ($address_unit_values as $address_unit_value) {

            if ($address_unit_value->before_address_unit_value_id) {

                $tree_address_unit_turn_index[$address_unit_value->id] = $tree_address_unit_turn_index[$address_unit_value->before_address_unit_value->id].'.nodes.'.$address_unit_value->id;
            } else {

                $tree_address_unit_turn_index[$address_unit_value->id] = $address_unit_value->id;
            }
        }

        foreach ($tree_address_unit_turn_index as $address_unit_value_id => $set_key) {

            $address_unit_value = $address_unit_values[$address_unit_value_id];

            if ($address_unit_value->before_address_unit_value_id) {

                $before_set_key = $tree_address_unit_turn_index[$address_unit_value->before_address_unit_value_id];

                $tmp = array_get($address_unit_value_infos, $before_set_key);

                $tmp['child_unit'] = $address_unit_value->address_unit_turn->address_unit;
                $tmp['child_turn'] = $address_unit_value->address_unit_turn;

                $address_unit_value_infos = array_set($address_unit_value_infos, $before_set_key, $tmp);
            }

            $tmp = [
                'unit' => $address_unit_value->address_unit_turn->address_unit,
                'turn' => $address_unit_value->address_unit_turn,
                'value' => $address_unit_value,
            ];

            $address_unit_value_infos = array_set($address_unit_value_infos, $set_key, $tmp);
        }
    }

    return render('estate/detail', [
        'estate' => $estate,
        'address_unit_value_infos' => $address_unit_value_infos,
    ]);
});

if_get('/estates/update/*', function ($estate_id)
{
    $estate = dao('estate')->find($estate_id);
    otherwise($estate->is_not_null(), 'estate not found');

    return render('estate/update', [
        'estate' => $estate,
    ]);
});

if_post('/estates/update/*', function ($estate_id)
{
    $estate = dao('estate')->find($estate_id);
    otherwise($estate->is_not_null(), 'estate not found');

    $estate->name = input('name');
    $estate->is_auto_manage_value = input('is_auto_manage_value');
    $estate->output_address_slot_id = input('output_address_slot_id');

    relationship_batch_load($estate, 'address_slots.address_unit_turns.address_unit');

    foreach ($estate->address_slots as $address_slot) {

        $address_slot->set_is_contain_output_address_unit();
    }

    return redirect('/estates');
});

if_post('/estates/delete/*', function ($estate_id)
{
    $estate = dao('estate')->find($estate_id);
    otherwise($estate->is_not_null(), 'estate not found');

    $estate->delete_relation_entities();
    $estate->delete();

    return redirect('/estates');
});
